package com.koron.css2.newProjectCenter.impl.material;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjMaterialCheckBean;
import com.koron.css2.newProjectCenter.mapper.PjMaterialCheckMapper;
import com.koron.css2.newProjectCenter.mapper.PubUpStoreMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Objects;

/**
 * 材料盘存单据情况
 */
public class PjMaterialCheck implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjMaterialCheckBean bean = JsonUtils.objectToPojo(req.getData(), PjMaterialCheckBean.class);
            // 创建盘存任务
            if (bean.getTodo() != null && bean.getTodo() == 2) {
                if (bean.getMaterialList().isEmpty())
                    return MessageBean.create(Constant.MESSAGE_INT_FAIL,"材料列表不可为空",null);
                // 仓库id不可为空
                if (bean.getStoreId() == null)
                    return MessageBean.create(Constant.MESSAGE_INT_FAIL,"请选择仓库",null);
                // 创建单据
                return createPjMaterialCheck(bean, factory, userInfo);
            }
            // 查询盘存信息
            // 1.分页获取盘存单据
            PjMaterialCheckMapper mapper = factory.getMapper(PjMaterialCheckMapper.class);
            PageHelper.startPage(bean.getPage(), bean.getPageCount());
            List<PjMaterialCheckBean> list = mapper.getList(bean);
            // 2.根据盘存单据id获取所有当前单据下的材料信息
            if (!list.isEmpty()) {
                for (PjMaterialCheckBean checkBean : list) {
                    checkBean.setMaterialList(mapper.getMaterialListByCheckId(checkBean.getId()));
                }
            }
            PageInfo info = new PageInfo(list);
            MessageBean<PageInfo> result = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
            result.setData(info);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
        }
    }

    /**
     * 创建盘存单据
     */
    private MessageBean<?> createPjMaterialCheck(PjMaterialCheckBean bean, SessionFactory factory, UserInfoBean userInfo) {
        // 根据仓库id获取仓库名称
        PubUpStoreMapper storeMapper = factory.getMapper(PubUpStoreMapper.class);
        String storeName = storeMapper.selectStoreNameById(bean.getStoreId());
        // 判断仓库名称是否为空
        if (storeName == null)
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,"仓库不存在",null);
        bean.setStoreName(storeName);
        bean.setCreateInfo(userInfo);
        bean.setStatus(true);
        // 创建单据
        PjMaterialCheckMapper mapper = factory.getMapper(PjMaterialCheckMapper.class);

        // 开始插入盘存明细
        String checkId = bean.getId();
        bean.getMaterialList().forEach(item -> {
            item.setCheckId(checkId);
            item.setCreateInfo(userInfo);
            item.setCheckStock(getMaterialStock(storeMapper, item.getMaterialId(), bean.getStoreId()));
            item.setCurrentStock(storeMapper.getQuantityByStoreIdAndMaterialId(bean.getStoreId(), item.getMaterialId()));
            // 判断当前库存是否与盘存库存一致
            item.setCheckStatus(Objects.equals(item.getCheckStock(), item.getCurrentStock()));
            if (!item.getCheckStatus()) {
                bean.setStatus(false);
            }
            item.setCreateInfo(userInfo);
        });

        mapper.insert(bean);
        // 批量插入盘存明细
        mapper.insertDetail(bean.getMaterialList());
        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"创建成功",null);
    }

    /**
     * 获取材料盘存库存情况
     */
    private Integer getMaterialStock(PubUpStoreMapper storeMapper, String materialId, String storeId) {
        // 1.获取当前仓库对应材料的原始库存量
        Integer originalQuantity = storeMapper.getOriginalQuantityByStoreIdAndMaterialId(storeId, materialId);
        if (originalQuantity == null)
            return 0;
        // 2.获取所有入库的材料数量
        // 2.1根据材料id以及仓库id获取所有入库总和
        Integer enterSum = storeMapper.getSumDocIdList(materialId, storeId, true);
        // 2.2根据材料id以及仓库id获取所有出库或报废遗失的总和
        Integer outSum = storeMapper.getSumDocIdList(materialId, storeId, false);
        if (enterSum != null)
            originalQuantity += enterSum;
        if (outSum != null)
            originalQuantity -= outSum;
        return originalQuantity;
    }
}
